
#**************************************************************************************

import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
import PyQt4.Qwt5 as Qwt

import numpy


# This is the number of points which are in a data set sent by the microcontroller.
OSC_DATA_SET_SIZE = 200


#======================================================================================
# This class draws an oscilloscope-like plot on the screen. 

OSC_MAXTIME = 5.0

class OscopePlot (Qwt.QwtPlot):  

	#----------------------------------------------------------------------------------

	def __init__ (self, curveSymbol = Qwt.QwtSymbol ()):  
		Qwt.QwtPlot.__init__ (self)  
		#self.setTitle ("<font size=1 color=darkblue>Ministrone Scope</font>")  
		self.setCanvasBackground (QtCore.Qt.black)      

		# Specify the grid; it will be dark green by default
		grid = Qwt.QwtPlotGrid ()  
		#grid.enableXMin(True)  
		#grid.enableYMin(True)  
		grid.setMajPen (QtGui.QPen (QtCore.Qt.darkGreen, 0, QtCore.Qt.DotLine))  
		grid.setMinPen (QtGui.QPen (QtCore.Qt.darkGreen, 0 , QtCore.Qt.DotLine))  
		grid.attach (self)  

		# Turn the display of the trigger level off until it's needed
		self.trigger_level = 0
		self.show_trigger_level = False

		# x-axis  
		self.setAxisTitle (Qwt.QwtPlot.xBottom, 
										"<font size=1 color=black>time (sec)</font>")  
		self.amplitudes = [0.0] * OSC_DATA_SET_SIZE       # zeros (OSC_DATA_SET_SIZE, Float)  

		# y-axis
		self.setAxisTitle (Qwt.QwtPlot.yLeft, 
										"<font size=1 color=black>A/D Output</font>")

		# curve  
		self.setAxisScale (Qwt.QwtPlot.yLeft, 0, 1023)
		self.setAxisScale (Qwt.QwtPlot.xBottom, 0, OSC_MAXTIME)

		self.amplitude_plot = Qwt.QwtPlotCurve ('A/D Output (bits)')  
		self.amplitude_plot.setPen (QtGui.QPen (QtCore.Qt.yellow))  
		self.amplitude_plot.setSymbol (Qwt.QwtSymbol (Qwt.QwtSymbol.Diamond,
									   QtGui.QBrush (QtCore.Qt.yellow),
									   QtGui.QPen (QtCore.Qt.black),
									   QtCore.QSize (7,7)))
		self.amplitude_plot.attach (self)  

		self.trigger_plot = Qwt.QwtPlotCurve ('Trigger Level')  
		#self.setAxisScale (Qwt.QwtPlot.yLeft, 0, 1023)
		#self.setAxisScale (Qwt.QwtPlot.xBottom, 0, OSC_MAXTIME)
		self.trigger_plot.setPen (QtGui.QPen (QtCore.Qt.red))  
		self.trigger_plot.attach (self)  


	#----------------------------------------------------------------------------------
	# This method sets up the parameters used to define the time axis. 

	def set_time_axis (self, time_per_div, divs_per_plot, pts_per_plot):
		plot_time = time_per_div * divs_per_plot
		time_per_point = plot_time / pts_per_plot
		self.timerange = numpy.arange (0.0, plot_time, time_per_point)
		self.setAxisScale (Qwt.QwtPlot.xBottom, 0, plot_time)


	#----------------------------------------------------------------------------------
	# This method draws a plot from a new set of data. 

	def updatePlot (self, new_values):  
		# Set the data to be plotted in the Channel 1 curve
		self.amplitude_plot.setData (self.timerange, new_values)

		# Make a line of data to be plotted as the trigger level line
		if self.show_trigger_level == True:
			self.trigger_plot.setData (self.timerange, 
									[self.trigger_level]*OSC_DATA_SET_SIZE)

		# /////////////////////////////////////////////////////////////////////////////
		print ("updatePlot: " + str (len (new_values)))
		# /////////////////////////////////////////////////////////////////////////////

		# This causes the plot to be drawn so it can be seen and admired
		self.replot ()  


	#----------------------------------------------------------------------------------
	# This method sets the trigger level and sets a flag so that the trigger level
	# will be displayed during a plot. 

	def set_trigger_level (self, new_level):
		self.show_trigger_level = True
		self.trigger_level = new_level


	#----------------------------------------------------------------------------------
	# This method turns off displaying of the trigger level. It is generally used when
	# the trigger mode is switched to manual. 

	def hide_trigger_level (self):
		self.show_trigger_level = False

